package stack;

/**
 * 自动扩容放到了Queue中 以下的数据结构就不重复写
 * 栈为先进后出的结构
 * 尾指针不动，只动头指针，新增数据上移头指针，获取数据下移头指针
 */

class Stack_Test{
    public static void main(String[] args) {
        Stack stack = new Stack(2);
        stack.add("1");
        stack.add("2");
        System.out.println(stack.get());
        stack.add("3");
    }
}

public class Stack {

    // 首指针
    private static int front;

    // 尾指针
    private static int rear;

    // 数组模拟栈
    private static Object[] value;

    // 初始容量
    private static int DEFAULT_SIZE_FACTOR = 4<<2;

    // 元素个数
    private static int size;

    //判断是否满
    private static final Boolean isFull() { return front == DEFAULT_SIZE_FACTOR -1; }

    // 判断是否空
    private static final Boolean isEmpty() { return front == -1; }

    // 无参构造
    public Stack() {
       rear = 0;
       front = -1;
       size = 0;
       value = new Object[DEFAULT_SIZE_FACTOR];
    }

    // 传入默认大小构造
    public Stack(int SIZE_FACTOR) {
        rear = 0;
        size = 0;
        front = -1;
        DEFAULT_SIZE_FACTOR = SIZE_FACTOR;
        value = new Object[DEFAULT_SIZE_FACTOR];
    }

    // 添加数据
    static final public void add(Object v){
        if (isFull()){throw new RuntimeException("栈已满，不能添加数据");}
        front ++ ;
        value[front] = v;
    }

    // 获取数据
    static final public Object get(){
        if (isEmpty()){throw new RuntimeException("栈为空");}
        Object o = value[front];
        front -- ;
        return o;
    }
}
